package com.rs.game.player.content.commands;

import com.rs.Settings;
import com.rs.cache.loaders.ItemDefinitions;
import com.rs.game.World;
import com.rs.game.WorldTile;
import com.rs.game.item.Item;
import com.rs.game.player.Player;
import com.rs.game.player.actions.DonatorActions;
import com.rs.game.player.content.Commands;
import com.rs.game.player.content.ForumRights;
import com.rs.game.player.content.GESearch;
import com.rs.game.player.content.Magic;
import com.rs.game.player.content.TicketSystem;
import com.rs.game.player.controlers.Wilderness;
import com.rs.utils.Logger;
import com.rs.utils.Misc;
import com.rs.utils.Utils;
import com.runetoplist.RuneTopList;
import com.runetoplist.callbacks.VoteRewardCallback;
import com.runetoplist.model.VoteReward;

public class NormalPlayer {

	/*
	 * Handles Normal Player Commands
	 */
	public static int getWildLevel(final Player player) {
		if (player.getY() > 9900)
			return (player.getY() - 9912) / 8 + 1;
		return (player.getY() - 3520) / 8 + 1;
	}

	public static boolean processCommand(final Player player, String[] cmd, boolean console, boolean clientCommand) {
		if (clientCommand) {

		} else {
			String message;
			String name;

			switch (cmd[0]) {

			case "getprice":	
				if (cmd.length < 2) {
					player.getPackets().sendGameMessage("Use: ::getprice (name)");
					return true;
				}
				try {
					name = "";
					for (int i = 1; i < cmd.length; i++) {
						name += cmd[i] + ((i == cmd.length - 1) ? "" : " ");
					}
					GESearch.searchForPrice(player, name);
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::getprice (name)");
				}
				return true;

			case "timeonline":
			case "playtime":
				player.getIntervalTimer().UpdateTotalTime();
				player.getPackets().sendGameMessage("You have played for: " +player.getIntervalTimer().getPlayTime());
				return true;
				
			case "resett":
				player.setHasBounterHunterTarget(false);
				return true;
			
			case "tm":
				player.setTargetMeter(player.getTargetMeter() + 1);
				return true;
				
				
			case "resettm":
				player.setTargetMeter(0);
				return true;
				
			case "info": 
				System.out.println("EP: "+player.getEarnedPotential());
				System.out.println("TM: "+player.getTargetMeter());
				System.out.println("Target: "+player.hasBountyHunterTarget());
				System.out.println("WildyLevel: "+player.getWildernessLevel());
				System.out.println("Target:"+player.hasBountyHunterTarget());
				System.out.println("TargetName:"+Misc.formatPlayerNameForProtocol(player.getBountyHunterTarget().getUsername()));

				return true;
				
			case "canpvp":
				if (player.isCanPvp() == false) {
					player.setCanPvp(true);
					player.getPackets().sendGameMessage("<col=FF0000>You can now PvP (Safe) everywhere!");
					return true;
				} else {
					player.setCanPvp(false);
					player.getPackets().sendGameMessage("<col=FF0000>You disabled PvPing everywhere!");
				}
				return true; 

			case "go": 
				player.getControlerManager().startControler("BountyHunterFinal");
				return true;
				
			case "reset":
				player.setHasBounterHunterTarget(false);
				return true;
				
				/*
				 * Spawn an item
				 * 
				 * Usage: ::item itemId amount Example: ::item 995 10
				 */
			case "item":
				if (cmd.length < 2) {
					player.getPackets().sendGameMessage("Use: ::item itemId (option:amount)");
					return true;
				}
				try {
					int itemId = Integer.valueOf(cmd[1]);
					ItemDefinitions defs = ItemDefinitions.getItemDefinitions(itemId);
					if (defs.isLended()) {
						return false;
					}
					if (defs.isOverSized()) {
						player.getPackets().sendGameMessage("The item appears to be oversized.");
						return false;
					}
					name = defs == null ? "" : defs.getName().toLowerCase();
					player.getInventory().addItem(itemId, cmd.length >= 3 ? Integer.valueOf(cmd[2]) : 1);
					player.getPackets().sendGameMessage("You spawned <col=FF0000>" + (cmd.length >= 3 ? cmd[2] : "1") + " " + defs.getName().toLowerCase() + (cmd.length >= 3 ? "s" : "") + "</col>.");
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::item itemId (optional:amount)");
				}
				return true;
				
			case "skip":
			case "resettask":
			case "newtask":
			case "skiptask": 
				if (player.getSlayerManager().getCurrentTask() == null) {
					player.getPackets().sendGameMessage("You currently don't have a task.");
					return false;
				}
				if (player.getInventory().getCoinsAmount() <= 100000) {
					player.getPackets().sendGameMessage("You need at least 100.000 coins in order to remove your current task.");
					return false;
				} else {
					player.getInventory().removeItemMoneyPouch(new Item(995, 100000));
					player.getSlayerManager().resetTask(false, true);
					player.getPackets().sendGameMessage("You skipped your current task for 100.000 coins.");
				}
				return true;

			case "switchitemslook":
				player.switchItemsLook();
				player.getInventory().refresh();
				player.getPackets().sendGameMessage(
						"You are now playing with "
								+ (player.isOldItemsLook() ? "old" : "new")
								+ " item looks.");
				return true;

				/*
				 * Set your account pin
				 * 
				 * Usage: ::setpin pin
				 * Example: ::setpin 1234
				 */

				/**TODO:	case "setpin":
				try {
					if (!player.accountPinSet) {
						if (Integer.parseInt(cmd[1]) >= 1000 && Integer.parseInt(cmd[1]) <= 9999) {
							player.accountPinSet = true;
							player.setAccountPin(Integer.parseInt(cmd[1]));
							player.getPackets().sendGameMessage("<col=FF0000>You have set your account pin to '" + cmd[1] + "'.");
						} else {
							player.getPackets().sendGameMessage("You must set a 4 digit pin");
						}
					}
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Invalid pin number!");
				}
				return true;*/

				/*
				 * Make the user a donator or extreme donator if they donated
				 * 
				 * Usage: ::donated
				 * Example: ::donated
				 */
			case "donated":
				ForumRights.setRights(player);
				return true;

				/*
				 * Send a yell across the server
				 * 
				 * Usage: ::yell message goes here
				 * Example: ::yell this is a yell
				 */

			case "yell":
				message = "";
				for (int i = 1; i < cmd.length; i++) {
					message += cmd[i] + ((i == cmd.length - 1) ? "" : " ");
				}
				Commands.sendYell(player, Utils.fixChatMessage(message), false);
				return true;

				/*
				 * Teleport the player home
				 * 
				 * Usage: ::home
				 * Example: ::home
				 */
			case "home":
				if (player.getControlerManager().getControler() instanceof Wilderness) {
					if (getWildLevel(player) > 20) {
						player.getPackets().sendGameMessage("A mysterious force prevents you from teleporting.");
						return false;
					}
					if (player.getTeleBlockDelay() > Utils.currentTimeMillis()) {
						player.getPackets().sendGameMessage("A mysterious force prevents you from teleporting.");
						return false;
					}
				}
				Magic.HomeTeleport(player, 2193, 3184, 0);
				return true;

			case "dung": 
				if (player.getControlerManager().getControler() instanceof Wilderness) {
					if (getWildLevel(player) > 20) {
						player.getPackets().sendGameMessage("A mysterious force prevents you from teleporting.");
						return false;
					}
					if (player.getTeleBlockDelay() > Utils.currentTimeMillis()) {
						player.getPackets().sendGameMessage("A mysterious force prevents you from teleporting.");
						return false;
					}
				}
				player.setNextWorldTile(new WorldTile(3450, 3720, 0));
				player.getPackets().sendGameMessage("You teleported to Daemonheim.");
				return true; 

				/*
				 * Check if you voted
				 * 
				 * Usage: ::check OR ::voted OR ::claim
				 * Example: ::check OR ::voted OR ::claim
				 */
			case "claimvote":
			case "claimvoted":
			case "reward":
			case "check":
			case "voted":
			case "claim":
				RuneTopList.checkRewards(player.getUsername().toLowerCase().replaceAll(" ", "_"), new VoteRewardCallback() {					
					@Override
					public void callback(Object obj) {
						Player p = (Player)obj;
						if(getVoteRewards().size() == 0){
							player.getPackets().sendGameMessage("You haven't voted! Do ::vote to vote.");
						} else {
							for(VoteReward reward : getVoteRewards()){
								switch (reward.getRewardid()) {
								case 173:
										player.getBank().addItem(989, 1, true);
										player.setVotes(player.getVotes() + 3);
										World.sendWorldMessage("<img=7><col=FF0000>News: " + Utils.formatPlayerNameForDisplay(player.getDisplayName()) + " has just claimed their vote reward. Type ::vote to vote now!</col>", false);
										p.getPackets().sendGameMessage("The reward has been sent to your bank.");
										Logger.globalLog(player.getUsername(), player.getLastIP(), new String(" has claimed a crystal key as a voting reward."));
										break;
								case 174:
										player.getBank().addItem(24155, 1, true);
										player.setVotes(player.getVotes() + 3);
										World.sendWorldMessage("<img=7><col=FF0000>News: " + Utils.formatPlayerNameForDisplay(player.getDisplayName()) + " has just claimed their vote reward. Type ::vote to vote now!</col>", false);
										p.getPackets().sendGameMessage("The reward has been sent to your bank.");
										Logger.globalLog(player.getUsername(), player.getLastIP(), new String(" has claimed A Double Spin Ticket as a voting reward."));
										break;							
								case 176:
										player.getBank().addItem(995, 2000000, true);
										player.setVotes(player.getVotes() + 3);
										World.sendWorldMessage("<img=7><col=FF0000>News: " + Utils.formatPlayerNameForDisplay(player.getDisplayName()) + " has just claimed their vote reward. Type ::vote to vote now!</col>", false);
										p.getPackets().sendGameMessage("The reward has been sent to your bank.");
										Logger.globalLog(player.getUsername(), player.getLastIP(), new String(" has claimed 2M coins as a voting reward."));
										break;
								case 177:
										player.getBank().addItem(18831, 5, true);
										player.setVotes(player.getVotes() + 3);
										World.sendWorldMessage("<img=7><col=FF0000>News: " + Utils.formatPlayerNameForDisplay(player.getDisplayName()) + " has just claimed their vote reward. Type ::vote to vote now!</col>", false);
										p.getPackets().sendGameMessage("The reward has been sent to your bank.");
										Logger.globalLog(player.getUsername(), player.getLastIP(), new String(" has claimed 5 Frost Dragon Bones as a voting reward."));
										break;
								default:
									player.getPackets().sendGameMessage("Reward not found.");
									break;
								}
							}
						}						
					}
				});
				return true;


				/*
				 * Empty the players inventory
				 * 
				 * Usage: ::clearinv
				 * Example: ::clearinv
				 */
			case "clearinv":
				player.getInventory().reset();
				return true;

				/*
				 * Change your password
				 * 
				 * Usage: ::changepass newpasshere
				 * Example: ::changepass mynewpassword
				 */
			case "changepass":
				message = "";
				for (int i = 1; i < cmd.length; i++) {
					message += cmd[i] + ((i == cmd.length - 1) ? "" : " ");
				}
				if (message.length() > 15 || message.length() < 5) {
					player.getPackets().sendGameMessage("Your password must be between 5-14 characters.");
					return true;
				}
				player.getPackets().sendGameMessage("You changed your password to \"" + message + "\".");
				player.setPassword(message);
				
				return true;

				/*
				 * Tell the player how many players are online
				 * 
				 * Usage: ::players
				 * Example: ::players
				 */
			case "players":
				player.getInterfaceManager().sendInterface(275);
				int number = 0;
				for (int i = 0; i < 310; i++) {
					player.getPackets().sendIComponentText(275, i, "");
				}
				for (Player players : World.getPlayers()) {
					if (players == null) {
						continue;
					}
					number++;
					String titles = "";
					if (players.getRights() == 0) {
						if (player.getUsername().equalsIgnoreCase("server")) {
							titles = "";
						}
						titles = "[Player]";
					}
					if (players.isSupporter()) {
						titles = "[<img=12><col=6666C9>Server Support</col>]";
					}
					if (players.isRespected()) {
						titles = "[<img=2><col=6666C9>Respected Member</col>]";
					}
					if (players.isGraphicDesigner()) {
						titles = "[<img=9><col=6666FF>Graphic Designer</col>]";
					}
					if (players.isDonator() && !players.isExtremeDonator()) {
						titles = "[<img=5><col=FF0000>Donator</col>]";
					}
					if (players.isExtremeDonator()) {
						titles = "[<img=4><col=02B41D>Extreme Donator</col>]";
					}
					if (players.getRights() == 1 && players.isStaff()) {
						titles = "[<img=0><col=008B00>Mod</col>]";
					}
					if (players.getRights() == 2 && players.isStaff()) {
						titles = "[<img=1><col=ff0000>Administrator</col>]";
					}
					player.getPackets().sendIComponentText(275, (13 + number), titles + "" + players.getDisplayName());
				}
				player.getPackets().sendIComponentText(275, 1, "Styx Players");
				player.getPackets().sendIComponentText(275, 10, " ");
				player.getPackets().sendIComponentText(275, 11, "Who's Online");
				player.getPackets().sendIComponentText(275, 12, "There are current <col=FF0000>" + World.getPlayers().size() + "</col> players online.");
				player.getPackets().sendGameMessage("There are currently <col=FF0000>" + World.getPlayers().size() + "</col> players online.");
				return true;

				/*
				 * Checks amount of Penguins found
				 * 
				 *  Usage: ::penguins
				 *  Example ::penguins
				 */
				/**TODO:		case "penguins":
			player.getPackets().sendGameMessage("You have found a total of " +player.gettotalpeng()+"/7 penguins.");
				return true;*/

				/*
				 * Set your yell color
				 * 
				 * Usage: ::yellcolor
				 * Example: ::yellcolor
				 */
			case "yellcolor":
				if (!player.isExtremeDonator()) {
					player.getPackets().sendGameMessage("You must be an extreme donator to use this feature.");
					return true;
				}
				player.getPackets().sendRunScript(109, new Object[] { "Please enter the yell color in HEX format." });
				player.getTemporaryAttributes().put("yellcolor", Boolean.TRUE);
				return true;
				/*
				 * Opens a ticket
				 * 
				 * Usage: ::ticket
				 * Example: ::ticket
				 */
			case "ticket":
				if (player.getMuted() > Utils.currentTimeMillis()) {
					player.getPackets().sendGameMessage("You're temporarily muted. Recheck in 48 hours.");
					return true;
				}
				TicketSystem.requestTicket(player);
				return true;

				/*
				 * Teleport to the donator zone if donator
				 * 
				 * Usage: ::donatorzone
				 * Example: ::donatorzone
				 */
			case "donatorzone":
			case "dz":
				DonatorActions.checkDonator(player);
				break;

				/*
				 * Opens a URL to the vote page
				 * 
				 * Usage: ::vote
				 * Example: ::vote
				 */
			case "vote":
				player.getPackets().sendOpenURL(Settings.VOTE_LINK);
				break;

				/*
				 * Opens a URL to the donation page
				 * 
				 * Usage: ::donate
				 * Example: ::donate
				 */
			case "donate":
				player.getPackets().sendOpenURL(Settings.DONATE_LINK);
				break;

				/*
				 * Opens an interface with all the commands
				 * 
				 * Usage: ::commands OR ::cmd
				 * Example: ::commands OR ::cmd
				 */
			case "cmd":
			case "commands":
				player.getInterfaceManager().sendInterface(275);
				for (int i = 0; i < 310; i++) {
					player.getPackets().sendIComponentText(275, i, "");
				}
				player.getPackets().sendIComponentText(275, 1, "Phoenix Commands");

				player.getPackets().sendIComponentText(275, 11, "Normal Player Commands");
				player.getPackets().sendIComponentText(275, 12, "----------------------------------------");
				player.getPackets().sendIComponentText(275, 13, "::players");
				player.getPackets().sendIComponentText(275, 14, "::changepass");
				player.getPackets().sendIComponentText(275, 15, "::clearinv");
				player.getPackets().sendIComponentText(275, 16, "::voted");
				player.getPackets().sendIComponentText(275, 17, "::home");
				player.getPackets().sendIComponentText(275, 18, "::ticket");
				player.getPackets().sendIComponentText(275, 19, "::donated");
				player.getPackets().sendIComponentText(275, 20, "----------------------------------------");

				player.getPackets().sendIComponentText(275, 22, "Donator Commands");
				player.getPackets().sendIComponentText(275, 23, "----------------------------------------");
				player.getPackets().sendIComponentText(275, 24, "::yell");
				player.getPackets().sendIComponentText(275, 25, "::donatorzone");
				player.getPackets().sendIComponentText(275, 26, "----------------------------------------");

				player.getPackets().sendIComponentText(275, 28, "Extreme Donator Commands");
				player.getPackets().sendIComponentText(275, 29, "----------------------------------------");
				player.getPackets().sendIComponentText(275, 30, "::bank");

				if (player.getRights() >= 1 && player.isStaff()) {
					player.getPackets().sendIComponentText(275, 32, "Moderator Commands");
					player.getPackets().sendIComponentText(275, 33, "----------------------------------------");
					player.getPackets().sendIComponentText(275, 34, "::mute");
					player.getPackets().sendIComponentText(275, 35, "::kick");
					player.getPackets().sendIComponentText(275, 36, "::jail");
					player.getPackets().sendIComponentText(275, 37, "::sendhome");
					player.getPackets().sendIComponentText(275, 38, "::ticket");
					player.getPackets().sendIComponentText(275, 39, "::finishticket");
					player.getPackets().sendIComponentText(275, 40, "----------------------------------------");
				}

				if (player.getRights() == 2 && player.isStaff()) {
					player.getPackets().sendIComponentText(275, 42, "Administrator Commands");
					player.getPackets().sendIComponentText(275, 43, "----------------------------------------");
					player.getPackets().sendIComponentText(275, 44, "::ban");
					player.getPackets().sendIComponentText(275, 45, "::unban");
					player.getPackets().sendIComponentText(275, 46, "::giveitem");
					player.getPackets().sendIComponentText(275, 47, "::promote");
					player.getPackets().sendIComponentText(275, 48, "::demote");
					player.getPackets().sendIComponentText(275, 49, "::tele");
					player.getPackets().sendIComponentText(275, 50, "::unmute");
					player.getPackets().sendIComponentText(275, 51, "::unjail");
					player.getPackets().sendIComponentText(275, 52, "::givespins");
					player.getPackets().sendIComponentText(275, 53, "::item");
					player.getPackets().sendIComponentText(275, 54, "::getid");
					player.getPackets().sendIComponentText(275, 55, "::teleto");
					player.getPackets().sendIComponentText(275, 56, "::update");
					player.getPackets().sendIComponentText(275, 57, "::teletome");
					player.getPackets().sendIComponentText(275, 58, "::god");
					player.getPackets().sendIComponentText(275, 59, "::mypos");
					player.getPackets().sendIComponentText(275, 60, "::givegfx");
					player.getPackets().sendIComponentText(275, 61, "::givedonator");
					player.getPackets().sendIComponentText(275, 62, "::giveexdonator");
					player.getPackets().sendIComponentText(275, 63, "::givedev");
					player.getPackets().sendIComponentText(275, 64, "::takegfx");
					player.getPackets().sendIComponentText(275, 65, "::takedonator");
					player.getPackets().sendIComponentText(275, 66, "::takeexdonator");
					player.getPackets().sendIComponentText(275, 67, "::takedev");
					player.getPackets().sendIComponentText(275, 68, "----------------------------------------");
				}
				return true;
			}
		}
		return false;
	}
}